package so.shanku.essential.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.*;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.umeng.socialize.bean.SocializeConfig;
import com.umeng.socialize.sso.UMSsoHandler;
import net.tsz.afinal.annotation.view.ViewInject;
import net.tsz.afinal.json.JsonMap;
import net.tsz.afinal.json.JsonParseHelper;
import so.shanku.essential.R;
import so.shanku.essential.activity.base.BaseUIActivity;
import so.shanku.essential.adapter.ProductPicAdapter;
import so.shanku.essential.adapter.RelationProductAdapter;
import so.shanku.essential.app.MyApplication;
import so.shanku.essential.getdata.*;
import so.shanku.essential.utils.*;
import so.shanku.essential.view.BadgeView;
import so.shanku.essential.view.CircleImageView;
import so.shanku.essential.view.GridViewNoScroll;
import so.shanku.essential.view.InsideViewPager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Jaelyn on 2015/9/10.
 * 商品详情
 */
public class ProductDetailActivity extends BaseUIActivity {
    @ViewInject(id = R.id.ptr_scrollview)
    private PullToRefreshScrollView ptr_scrollview;
    /**
     * content view
     */
    @ViewInject(id = R.id.content_info_layout)
    private LinearLayout content_info_layout;

    /**
     * 店铺layout
     */
    @com.lidroid.xutils.view.annotation.ViewInject(R.id.base_brand_layout)
    private LinearLayout base_brand_layout;

    @com.lidroid.xutils.view.annotation.ViewInject(R.id.iv_head)
    private ImageView brandLogo_iv;
    @com.lidroid.xutils.view.annotation.ViewInject(R.id.tv_name)
    private TextView brand_name_tv;
    @com.lidroid.xutils.view.annotation.ViewInject(R.id.tv_msg)
    private TextView brand_discri_tv;

    /**
     * 动画的imageview
     */
    @com.lidroid.xutils.view.annotation.ViewInject(R.id.iv_anim_collection)
    private ImageView iv_anim_collection;

    @com.lidroid.xutils.view.annotation.ViewInject(R.id.home_goods_pic_vp)
    private InsideViewPager home_goods_pic_vp; //商品的图片

    /**
     * 剩余商品数量
     */
    @com.lidroid.xutils.view.annotation.ViewInject(R.id.goods_retain_tv)
    private TextView goods_retain_tv;
    /**
     * 刷新图下第一行
     */
    @com.lidroid.xutils.view.annotation.ViewInject(R.id.productBrand_tv)
    private TextView productBrand_tv;
    /**
     * 刷新图下第二行
     */
    @com.lidroid.xutils.view.annotation.ViewInject(R.id.brand_name_tv)
    private TextView product_discribe_tv;

    /**
     * 现价
     */
    @com.lidroid.xutils.view.annotation.ViewInject(R.id.current_price_tv)
    private TextView current_price_tv;
    /**
     * 查看详情
     */
    @com.lidroid.xutils.view.annotation.ViewInject(R.id.promotion_tv)
    private TextView promotion_tv;

    /**
     * 原价
     */
    @com.lidroid.xutils.view.annotation.ViewInject(R.id.original_price_tv)
    private TextView original_price_tv;

    /**
     * 是否分享
     */
    @com.lidroid.xutils.view.annotation.ViewInject(R.id.share_iv)
    private TextView share_iv;


    /**
     * 收藏人数
     */
    @ViewInject(id = R.id.collect_tv, click = "collect_tv_click")
    private TextView collect_tv;
    /**
     * 联系客服
     */
    @ViewInject(id = R.id.custom_service_tv, click = "custom_service_tv_click")
    private TextView custom_service_tv;

    /**
     * 加入购物车
     */
    @ViewInject(id = R.id.add_cart_tv, click = "add_cart_tv_click")
    private TextView add_cart_tv;
    /**
     * 店铺
     */
    @ViewInject(id = R.id.shop_tv, click = "shop_tv_click")
    private TextView shop_tv;


    @com.lidroid.xutils.view.annotation.ViewInject(R.id.relation_goods_layout)
    private LinearLayout relation_goods_layout;

    @ViewInject(id = R.id.relation_product_gv)
    private GridViewNoScroll relation_product_gv;

    /**
     * 下拉刷新Gridview适配器
     */
    private RelationProductAdapter productDetailAdapter;

    /**
     * 商品图片展示图
     */
    private ProductPicAdapter productPicAdapter;

    @com.lidroid.xutils.view.annotation.ViewInject(R.id.view_ind_layout)
    private LinearLayout view_ind_layout;     //指示器

    /**
     * 商品评论
     */
    /**
     * 商品评论大布局
     */
    @com.lidroid.xutils.view.annotation.ViewInject(R.id.goods_comments_layout)
    private LinearLayout goods_comments_layout;
    /**
     * 查看所有评论
     */
    @com.lidroid.xutils.view.annotation.ViewInject(R.id.scan_all_comments_tv)
    private TextView scan_all_comments_tv;

    /**
     * 评论提示
     */
    @com.lidroid.xutils.view.annotation.ViewInject(R.id.goods_comments_tv)
    private TextView goods_comments_tv;
    /**
     * 用户头像
     */
    @com.lidroid.xutils.view.annotation.ViewInject(R.id.user_photo_iv)
    private CircleImageView user_photo_iv;
    /**
     * 用户名
     */
    @com.lidroid.xutils.view.annotation.ViewInject(R.id.userName_tv)
    private TextView userName_tv;

    /**
     * 评论内容
     */
    @com.lidroid.xutils.view.annotation.ViewInject(R.id.userComments_tv)
    private TextView userComments_tv;

    @com.lidroid.xutils.view.annotation.ViewInject(R.id.create_time_tv)
    private TextView create_time_tv;

    /**
     * 评论中 商品尺码 颜色
     */
    @com.lidroid.xutils.view.annotation.ViewInject(R.id.goods_color_size_tv)
    private TextView goods_color_size_tv;

    @com.lidroid.xutils.view.annotation.ViewInject(R.id.product_detail_layout)
    private RelativeLayout product_detail_layout;

    private String productId;//商品id
    private String shopId;//商家Id
    private String classfyId;//商品类别
    private JsonMap goodsDatas;
    /**
     * 相关商品数据
     */
    private List<JsonMap<String, Object>> dataList;
    private int nPage = 0;
    private List<JsonMap<String, Object>> productPics;//商品图片集合
    private BadgeView shopping_cart_badge;//购物车

    /**
     * 标记是否请求到数据,
     */
    private boolean isRequestSuccess = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_detail);
        setMenuRes();
        relation_product_gv.setOnItemClickListener(onItemClickListener);
        ptr_scrollview.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
        ptr_scrollview.setOnRefreshListener(loadMoreListener);
        productPicAdapter = new ProductPicAdapter(mContext);
        addHead();
        productId = getIntent().getStringExtra(Constant.ID);
        productDetailAdapter = new RelationProductAdapter(this);
        productDetailAdapter.setShowDiscri(true);
        relation_product_gv.setAdapter(productDetailAdapter);
        getGoodsDetial();
    }


    /**
     * 商品点击
     */
    private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            JsonMap map = dataList.get(position);
            String goods_id = map.getStringNoNull("ProductId");
            seeProductDetail(goods_id);
        }
    };

    //设置头部右上角购物车
    public void setMenuRes() {
        menu_layout.removeAllViews();
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
        ImageView imageView1 = new ImageView(mContext);
        imageView1.setOnClickListener(menuClick);
        imageView1.setTag(1);
        imageView1.setImageResource(R.drawable.shopcart_white);
        imageView1.setPadding(getResources().getDimensionPixelSize(R.dimen.comon_half_margin), 0, getResources().getDimensionPixelSize(R.dimen.comon_half_margin), 0);
        menu_layout.addView(imageView1, params);
        ImageView imageView2 = new ImageView(mContext);
        imageView2.setOnClickListener(menuClick);
        imageView2.setTag(2);
        imageView2.setImageResource(R.drawable.share_white);
        imageView2.setPadding(getResources().getDimensionPixelSize(R.dimen.comon_half_margin), 0, getResources().getDimensionPixelSize(R.dimen.comon_half_margin), 0);
        menu_layout.addView(imageView2, params);

        shopping_cart_badge = new BadgeView(this, imageView1);
        shopping_cart_badge.setTextSize(10);
    }

    //购物车的点击事件
    View.OnClickListener menuClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int tag = (Integer) v.getTag();
            if (tag == 1) {
                jumpToShopCart();
            } else if (tag == 2) {
                share_iv_click(v);
            }
        }
    };


    //购物车接口
//    private void getShopCartNum() {
//        Map<String, Object> params = new HashMap<String, Object>();
//
//        params.put("UserAccount", Utils.getUserAccount(this));
//        GetDataQueue queue = new GetDataQueue();
//        queue.setCallBack(callBack);
//        queue.setActionName(GetDataConfing.GetAction_GetShoppingCartCount);
//        queue.setWhat(GetDataConfing.What_GetShoppingCartCount);
//        queue.setParamsNoJson(params);
//        getDataUtil.getData(queue);
//    }

    public void refreshShoppingCartNum() {
        int shopCartNum = MyApplication.getInstance().getShopcart_num();
        if (shopCartNum > 99) {
            shopping_cart_badge.setText("...");
            shopping_cart_badge.show();
        } else if (shopCartNum == 0) {
            shopping_cart_badge.hide();
        } else {
            shopping_cart_badge.setText("" + shopCartNum);
            shopping_cart_badge.show();
        }

    }

    /**
     * 刷新监听
     */
    private PullToRefreshBase.OnRefreshListener2 loadMoreListener = new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
        @Override
        public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {

        }

        @Override
        public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
            getRelationGoods();
        }
    };

    /**
     * 获取商品详情    * {Id:15973}
     */
    private void getGoodsDetial() {
        loadingToast.show();
        Map<String, Object> params = new HashMap<String, Object>();

        if (Utils.isHasLogin(this)) {
            params.put("userId", Utils.getUserId(this));
        }
        params.put("productId", productId);
        GetDataQueue queue = new GetDataQueue();
        queue.setActionName(GetDataConfing.GetAction_GetProductDetails);
        queue.setParamsNoJson(params);
        queue.setCallBack(callBack);
        queue.setWhat(GetDataConfing.What_GetProductDetails);
        getDataUtil.getData(queue);
    }


    /**
     * 获取相关商品  {VendorId:165,CategoryId:1535,currentPage:1,pageSize:10}
     */
    private void getRelationGoods() {
        Map<String, Object> params = new HashMap<String, Object>();

        params.put("nPage", nPage + 1);
        params.put("shopId", shopId);
        params.put("classfyId", classfyId);
        params.put("productId", productId);
        GetDataQueue queue = new GetDataQueue();
        queue.setActionName(GetDataConfing.GetAction_GetProudctCategroyList);
        queue.setParamsNoJson(params);
        queue.setCallBack(callBack);
        queue.setWhat(GetDataConfing.What_GetProudctCategroyList);
        getDataUtil.getData(queue);

    }

    /**
     * 添加头部控件
     */
    public void addHead() {
        View viewHead = getLayoutInflater().inflate(R.layout.head_product_detail, null);
        content_info_layout.addView(viewHead, 0);
        ViewUtils.inject(this, viewHead);
    }

    @OnClick(R.id.base_brand_layout)
    public void base_brand_layout_click(View view) {
        if (goodsDatas == null) {
            return;
        }
        seeShopDetail(shopId);
    }

    /**
     * 查看商品特性
     *
     * @param view
     */
    @OnClick(R.id.product_detail_layout)
    public void product_detail_layout_click(View view) {
        if (goodsDatas == null) {
            return;
        }

        Intent intent = new Intent(mContext, UrlWebviewActivity.class);
        intent.putExtra(ExtraKeys.Key_Msg1, 9);
        intent.putExtra(ExtraKeys.Key_Msg2, productId);
        jumpTo(intent);
    }

    /**
     * 分享的点击事件
     *
     * @param view
     */
    @OnClick(R.id.scan_all_comments_tv)
    public void scan_all_comments_tv_click(View view) {
        if (goodsDatas == null) {
            return;
        }

        Intent intent = new Intent(mContext, ProductCommentsActivity.class);
        intent.putExtra(Constant.ID, productId);
        jumpTo(intent, false);
    }

    /**
     * 分享的点击事件
     *
     * @param view
     */
    @OnClick(R.id.share_iv)
    public void share_iv_click(View view) {
        if (goodsDatas == null) {
            return;
        }

        JsonMap jsonMap_temp = goodsDatas;
        String shareLogo = ((JsonMap) jsonMap_temp.getList_JsonMap("productPicList").get(0)).getStringNoNull("picPath");
        String shareTitle = jsonMap_temp.getStringNoNull("salesName");
        String shareContent = jsonMap_temp.getStringNoNull("brandName");
        String shareUrl = "Product/Index?productId=" + productId;

        MyApplication.getInstance().showShareCompont(this, shareLogo, shareUrl, shareTitle, shareContent);
    }

    /**
     * 进入店铺
     *
     * @param view
     */
    public void shop_tv_click(View view) {
        if (goodsDatas == null) {
            return;
        }
        seeShopDetail(shopId);
    }

    /**
     * 联系客服
     *
     * @param view
     */
    public void custom_service_tv_click(View view) {
        if (goodsDatas == null) {
            return;
        }
        seeShopDetail(shopId);
    }

    /**
     * 收藏的点击事件
     *
     * @param view
     */
    public void collect_tv_click(View view) {
        if (goodsDatas == null) {
            return;
        }

        if (Utils.isHasLogin(mContext)) {
            if (Utils.isFastClick()) {
                return;
            }
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("userId", Utils.getUserId(this));
            params.put("productId", productId);

            GetDataQueue queue = new GetDataQueue();
            queue.setActionName(GetDataConfing.Action_collectionProduct);
            queue.setParamsNoJson(params);
            queue.setCallBack(callBack);
            queue.setWhat(GetDataConfing.What_productAddUserAttention);
            getDataUtil.getData(queue);
        } else {
            gotoLogin();
        }
    }

    /**
     * 加入购物车的点击事件
     *
     * @param view
     */
    public void add_cart_tv_click(View view) {
        if (goodsDatas == null) {
            return;
        }

        if (Utils.isHasLogin(mContext)) {
            /**
             * 商品状态和上下架
             */
            boolean isEnable = goodsDatas.getBoolean("isEnable");
            if (isEnable) {
                int stock = goodsDatas.getInt("stock");
                if (stock < 1) {
                    toast.showToast(R.string.salve_over);
                    return;
                }
            } else {
                toast.showToast(R.string.product_off_shelf);
                return;
            }

            String goodsName = goodsDatas.getStringNoNull("salesName");

            if (goodsDatas.getList_JsonMap("specList").isEmpty()) {
                add2Cart(productId, goodsName);
            } else {
                String json_str = goodsDatas.getString("specList");

                Intent intent = new Intent(this, SelectGoodsParamsActivity.class);
                intent.putExtra(Constant.COLOR_JSON_STR, json_str);
                intent.putExtra(Constant.ID, productId);
                intent.putExtra(Constant.NAME, goodsName);
                intent.putExtra(Constant.GOODS_PRICE, goodsDatas.getStringNoNull("salePrice"));
                jumpTo(intent);
                overridePendingTransition(R.anim.push_bottom_in, R.anim.push_bottom_out);
            }
        } else {
            gotoLogin();
        }
    }

    /**
     * 不选规格直接添加
     */
    private void add2Cart(String goodsId, String goodsName) {
        loadingToast.show();
        loadingToast.update(R.string.add2cart_ing);
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", Utils.getUserId(this));
        params.put("productId", goodsId);
        params.put("speStr", "");//sizeId+","+colorId);
        params.put("amount", 1);
        GetDataQueue queue = new GetDataQueue();
        queue.setActionName(GetDataConfing.GetAction_AddShoppingCart);
        queue.setParamsNoJson(params);
        queue.setCallBack(callBack);
        queue.setWhat(GetDataConfing.What_AddShoppingCart);
        getDataUtil.getData(queue);
    }


    /**
     * 查看详情的点击事件
     */
//    public void scan_detail_tv_click(View view) {
//        Intent intent = new Intent(this, ProductAfterMarketDescActivity.class);
//        intent.putExtra(ExtraKeys.Key_Msg1, productId);
//        intent.putExtra(ExtraKeys.Key_Msg2, goodsDatas.getStringNoNull("shopName"));
//        startActivity(intent);
//    }
    @Override
    public void onResume() {
        super.onResume();
        refreshShoppingCartNum();
    }

    private IGetServicesDataCallBack callBack = new IGetServicesDataCallBack() {
        @Override
        public void onLoaded(GetDataQueue entity) {
            if (entity.isOk()) {
                if (ShowGetDataError.isCodeSuccess(mContext, entity.getInfo())) {
                    if (entity.what == GetDataConfing.What_GetProudctCategroyList) {
                        ptr_scrollview.onRefreshComplete();
                        List<JsonMap<String, Object>> data_server = JsonParseHelper.getJsonMap_List_JsonMap(entity.getInfo(), JsonKeys.Key_Info);
                        setData(data_server);
                        validateData(data_server);
                    } else if (entity.what == GetDataConfing.What_GetShoppingCartCount) {  //获取购物车数据
                        int num = JsonParseHelper.getJsonMap_List_JsonMap(entity.getInfo(),
                                JsonKeys.Key_Info).get(0).getInt("Count");
                        MyApplication.getInstance().setShopcart_num(num);
                        refreshShoppingCartNum();
                    } else if (entity.what == GetDataConfing.What_AddShoppingCart) {//添加购物车
                        JsonMap<String, Object> data = JsonParseHelper
                                .getJsonMap(entity.getInfo());
                        String msg = data.getString(JsonKeys.Key_Msg);
                        toast.showToast(msg);
                        MyApplication.getInstance().addShopCartNum();
                        refreshShoppingCartNum();
                    } else if (entity.what == GetDataConfing.What_GetProductDetails) {
                        goodsDatas = JsonParseHelper.getJsonMap_JsonMap(entity.getInfo(), JsonKeys.Key_Info);
                        refreshGoodsDetail();
                    } else if (entity.what == GetDataConfing.What_productAddUserAttention) { //收藏商品

                        JsonMap<String, Object> data = JsonParseHelper.getJsonMap_JsonMap(entity.getInfo(), JsonKeys.Key_Info);
                        boolean isCollected = data.getBoolean("isCollected");
                        toast.showToast(data.getStringNoNull("msg"));
                        collect_tv.setSelected(isCollected);
                        collect_tv.setText(isCollected ? R.string.collectioned : R.string.collection);
                        if (isCollected) {
                            startAnimation();
                        }
                    }
                } else {
                    if (entity.what == GetDataConfing.What_GetProudctCategroyList) {
                        ptr_scrollview.onRefreshComplete();
                    }
                }
            } else {
                if (entity.what == GetDataConfing.What_GetProudctCategroyList) {
                    ptr_scrollview.onRefreshComplete();
                }
                ShowGetDataError.showNetError(mContext);
            }
            loadingToast.dismiss();
        }

    };

    private void updateCollectionStatues(boolean b) {

        collect_tv.setSelected(b);
//        if (b) {
//            collectionNum++;
//        } else {
//            collectionNum--;
//        }
//        collect_tv.setText("" + collectionNum);
    }

    private void startAnimation() {
        Animation animout = AnimationUtils.loadAnimation(this, R.anim.zoom_exit);
        iv_anim_collection.setVisibility(View.VISIBLE);
        iv_anim_collection.startAnimation(animout);
        animout.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                iv_anim_collection.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

    }

    /**
     * 设置商品信息 并请求相关商品的信息
     */
    private void refreshGoodsDetail() {
        setCenter_title(goodsDatas.getStringNoNull("brandName"));

        shopId = goodsDatas.getStringNoNull("shopId");
        classfyId = goodsDatas.getStringNoNull("classfyId");

        /**
         * 店铺
         */
        brand_name_tv.setText(goodsDatas.getStringNoNull("shopName"));
        brand_discri_tv.setText(goodsDatas.getStringNoNull("shopDescri"));
        String brandLogoPath = goodsDatas.getStringNoNull("shopLogo");
        if (MyApplication.getInstance().getBitmapHashMap().containsKey(brandLogoPath)) {
            brandLogo_iv.setImageBitmap((MyApplication.getInstance().getBitmapHashMap().get(brandLogoPath)));
        } else {
            ImageLoader.getInstance().displayImage(brandLogoPath, brandLogo_iv, MyApplication.getInstance().getBrandDefaultOption(), new ImageLoaderBitmapCallBack());
        }

        /**
         * 商品图
         */
        productPics = goodsDatas.getList_JsonMap("productPicList");
        if (productPics.size() != 0) {
            home_goods_pic_vp.setVisibility(View.VISIBLE);
            productPicAdapter = new ProductPicAdapter(mContext, productPics);
            productPicAdapter.setOnItemClickListener(new ProductPicAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(int position, int index) {
                    String str = goodsDatas.getStringNoNull("productPicList");
                    goodsImage_click(str, index % productPics.size());
                }
            });
            productPicAdapter.setIndContainerView(view_ind_layout);
            home_goods_pic_vp.setAdapter(productPicAdapter);
            home_goods_pic_vp.setCurrentItem(productPics.size() * 100);

            home_goods_pic_vp.setOnPageChangeListener(productPicAdapter);
        } else {
            home_goods_pic_vp.setVisibility(View.GONE);
        }

        /**
         * 商品状态和上下架
         */
        boolean isEnable = goodsDatas.getBoolean("isEnable");
        if (isEnable) {
            int stock = goodsDatas.getInt("stock");
            if (stock < 4) {
                goods_retain_tv.setVisibility(View.VISIBLE);
                if (stock < 1) {
                    goods_retain_tv.setText(R.string.salve_over);
                    MyApplication.getInstance().startAnimation(goods_retain_tv);
                } else {
                    String strFormat = this.getString(R.string.product_retain);
                    String goods_retain_str = String.format(strFormat, stock + "");
                    goods_retain_tv.setText(goods_retain_str);
                    MyApplication.getInstance().startAnimation(goods_retain_tv);
                }
            } else {
                goods_retain_tv.setVisibility(View.INVISIBLE);
            }

        } else {
            goods_retain_tv.setVisibility(View.VISIBLE);
            goods_retain_tv.setText(R.string.salve_over);
            MyApplication.getInstance().startAnimation(goods_retain_tv);
        }

        /**
         * 商品名字和价格
         */
        productBrand_tv.setText(goodsDatas.getStringNoNull("brandName"));
        product_discribe_tv.setText(goodsDatas.getStringNoNull("salesName"));

        String marketPrice = goodsDatas.getStringNoNull("marketPice");
        String discountPrice = goodsDatas.getStringNoNull("salePrice");
        if (!TextUtils.isEmpty(discountPrice) && !discountPrice.equalsIgnoreCase("0") && !discountPrice.equalsIgnoreCase(marketPrice)) {
            current_price_tv.setText(StringUtil.getFormatMoneyWithSign(discountPrice));
            Utils.addDeleteLine(current_price_tv);
            original_price_tv.setText(StringUtil.getFormatMoneyWithSign(marketPrice));
            original_price_tv.setVisibility(View.VISIBLE);
        } else {
            current_price_tv.setText(StringUtil.getFormatMoneyWithSign(marketPrice));
            original_price_tv.setVisibility(View.GONE);
        }

        promotion_tv.setText(goodsDatas.getStringNoNull("promotionMsg"));
        /**
         * 商品评论
         */
        int productCommentsCount = goodsDatas.getInt("productCommentsCount", 0);
        if (productCommentsCount > 0) {
            goods_comments_layout.setVisibility(View.VISIBLE);
            JsonMap commentJson = (JsonMap) goodsDatas.getList_JsonMap("productComments").get(0);

            userName_tv.setText(commentJson.getStringNoNull("userName"));
            String stringFormat = getString(R.string.goods_comments);
            String msg = String.format(stringFormat, productCommentsCount);
            goods_comments_tv.setText(msg);
            Utils.setUserIcon(commentJson, user_photo_iv);
            userComments_tv.setText(commentJson.getStringNoNull("commentsContent"));
            goods_color_size_tv.setText(commentJson.getStringNoNull("speStrName"));
        } else {
            goods_comments_layout.setVisibility(View.GONE);
        }
        create_time_tv.setVisibility(View.GONE);

        boolean isCollected = goodsDatas.getBoolean("isCollected");
        collect_tv.setSelected(isCollected);
        collect_tv.setText(isCollected ? R.string.collectioned : R.string.collection);
        getRelationGoods();
    }


    private void setData(List<JsonMap<String, Object>> data_server) {
        if (nPage == 0) {
            dataList = data_server;
            productDetailAdapter.setDatas(data_server);
            relation_product_gv.setAdapter(productDetailAdapter);
        } else {
            dataList.addAll(data_server);
            productDetailAdapter.notifyDataSetChanged();
        }
        nPage++;
    }


    /**
     * 校验数据 显示无数据等
     * <p/>
     * 是否请求正常 网络错误且数据为空的话显示网络异常界面 提供刷新的接口
     *
     * @param data_server
     */
    private void validateData(List<JsonMap<String, Object>> data_server) {
        if (data_server.size() == 0) {
            ptr_scrollview.setMode(PullToRefreshBase.Mode.MANUAL_REFRESH_ONLY);
        } else {
            ptr_scrollview.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
        }
        if (dataList.isEmpty()) {
            relation_goods_layout.setVisibility(View.GONE);
            relation_product_gv.setVisibility(View.GONE);
        } else {
            //relation_product_gv.setBoolInterceptTouchEvent(true);
            relation_product_gv.setVisibility(View.VISIBLE);
            relation_goods_layout.setVerticalGravity(View.VISIBLE);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /**使用SSO授权必须添加如下代码 */
        UMSsoHandler ssoHandler = SocializeConfig.getSocializeConfig().getSsoHandler(requestCode);
        if (ssoHandler != null) {
            ssoHandler.authorizeCallBack(requestCode, resultCode, data);
        }
    }
}
